<template>
	<view class="address">
		<uv-navbar placeholder autoBack bgColor="#CA1C1D">
			<template v-slot:left>
				<view class="left_slot">
					<image src="@/static/images/back.png"></image>
				</view>
			</template>
			<template v-slot:center>
				<view class="nav_title">地址管理</view>
			</template>
		</uv-navbar>
		<view class="list">
			<view class="item" @click="selected(item)" v-for="item in data_list">
				<view class="info">
					<view class="name">{{item.name}}</view>
					<view class="phone">{{item.phone}}</view>
					<view class="icon" @click.stop="router(`/pages/my/address_change?id=${item.id}`)">
						<image src="@/static/images/m26.png"></image>
					</view>
				</view>
				<view class="address_deta">
					<view class="label" v-if="item.is_default">默认</view>
					<view class="value">{{item.full_address}}</view>
				</view>
				<view class="oper">
					<view class="left">
						<view class="icon" v-if="item.is_default">
							<image src="@/static/images/checked.png"></image>
						</view>
						<view class="icon" @click.stop="set_default(item.id)" v-else>
							<image src="@/static/images/checked_no.png"></image>
						</view>
						<text>默认地址</text>
					</view>
					<view class="icon" v-if="!item.is_default" @click.stop="delete_click(item.id)">
						<image src="@/static/images/m27.png"></image>
					</view>
				</view>
			</view>
			<view class="submit">
				<view @click="router('/pages/my/address_add')">添加新地址</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		addressLists,
		addressDefault,
		addressDel
	} from "@/request/api";
	import useBase from "@/hooks/useBase.js";
	const {
		router
	} = useBase();
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import {
		ref
	} from "vue";
	import useStore from "@/stores/index.js";
	const {
		useAddressStore
	} = useStore();
	const data_list = ref([]);
	const getList = async () => {
		let res = await addressLists();
		data_list.value = res.address;
	};
	const set_default = async (id) => {
		await addressDefault({
			id
		});
		uni.$uv.toast("操作成功");
		getList();
	};
	const delete_click = async (id) => {
		await addressDel({
			id
		});
		uni.$uv.toast("操作成功");
		getList();
	};
	let is_select;
	const selected = (item) => {
		if (!is_select) return false;
		useAddressStore.setAddress(item);
		uni.navigateBack();
	};
	onShow(() => getList());

	onLoad(options => is_select = options.is);
</script>

<style scoped lang="scss">
	.address {
		min-height: calc(100vh - var(--window-top) - var(--window-bottom));
		padding: 0rpx 25rpx;
		box-sizing: border-box;
		background-color: #f5f5f5;
		overflow: hidden;

		.left_slot {
			image {
				width: 43rpx;
				height: 36rpx;
			}
		}

		.nav_title {
			color: #F5F5F5;
			font-size: 36rpx;
		}

		.list {
			padding: 20rpx 20rpx 0;
			box-sizing: border-box;

			.item {
				border-radius: 20rpx;
				margin-bottom: 16rpx;
				padding: 0 30rpx;
				box-sizing: border-box;
				background: #FFFFFF;
				overflow: hidden;

				.info {
					padding-top: 32rpx;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.name {
						color: #333333;
						font-size: 32rpx;
						font-weight: bold;
					}

					.phone {
						flex: 1;
					}

					.icon {
						image {
							width: 40rpx;
							height: 40rpx;
						}
					}
				}

				.address_deta {
					padding: 30rpx 0;
					box-sizing: border-box;
					display: flex;

					.label {
						width: 60rpx;
						height: 30rpx;
						line-height: 30rpx;
						text-align: center;
						color: #FFFFFF;
						font-size: 20rpx;
						border-radius: 15rpx;
						margin-right: 20rpx;
						background: #CA1C1D;
					}

					.value {
						flex: 1;
						line-height: 40rpx;
						color: #999999;
						font-size: 28rpx;
						font-weight: bold;
					}
				}

				.oper {
					height: 80rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						display: flex;
						align-items: center;

						text {
							color: #666666;
							font-size: 24rpx;
							padding-left: 10rpx;
							text-align: center;
						}
					}

					.icon {
						image {
							width: 40rpx;
							height: 40rpx;
						}
					}
				}
			}

			.submit {
				padding: 0 10rpx;
				box-sizing: border-box;

				view {
					height: 88rpx;
					line-height: 88rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 32rpx;
					border-radius: 20rpx;
					background: #CA1C1D;
				}
			}
		}
	}
</style>